using System;
using System.Collections.Generic;
using SCG.SolarVengeanceEngine;
using SCG.General;

namespace SCG.SolarVengeance.Scenarios
{
    public class XMarksTheSpot : Scenario
    {
        //Build the Scenario
        public override void BuildScenario(int numPlayers, int capitalValues, int startingResources)
        {
            _morilla = CreateStarSystem(50, 50, 25, 50, 20, 200);
            _morilla.Name = "Morilla";
            CreateStarCluster(20, 5, 5, 95, 95, 10, 4);
            CreateStarCluster(20, 95, 5, 5, 95, 10, 4);
            int numNebula = Random(10, 20);
            while (numNebula > 0)
            {
                CreateNebula(Random(5, 95), Random(5, 95), 2, Random(10, 40));
                numNebula--;
            }
            int numWormholes = Random(4, 8);
            while (numWormholes > 0)
            {
                CreateWormhole(Random(5, 95), Random(5, 95), Random(5, 95), Random(5, 95));
                numWormholes--;
            }

            //Make sure Morilla is not assigned as a capital, and capital's aren't too close to it
            foreach (Player p in Game.Players)
            {
                do
                {
                    AssignCapital(p, new SCGRect(0, 0, 100, 100));
                }
                while (_morilla.Distance(p.Capital) < 20);
            }
        }

        //Scenario description
        public override string Description
        {
            get
            {
                return "The NomNoid Cluster is strangely X-shaped.  A heavy cluster of Stars lies in the middle of the X, with the fabled StarSystem Morilla at the very center.  " +
                    "Those who can capture and hold Morilla for 50 Impulses will be declared the winner.";
            }
        }

        //Custom VC
        public override bool VictoryReached()
        {
            if (_morilla.Owner != null)
            {
                if (_morilla.Owner == _lastOwner)
                {
                    _counter++;
                    if (_counter > 50)
                    {
                        _morilla.Owner.Victor = true;
                        return true;
                    }
                }
                else
                {
                    _lastOwner = _morilla.Owner;
                    _counter = 0;
                }
            }
            return base.VictoryReached();
        }

        //private members
        private StarSystem _morilla;
        private Player _lastOwner = null;
        private int _counter = 0;
    }
}
